package com.liuxl.wqdc.repository;

import com.liuxl.wqdc.entity.Record;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

@Component
public class BatchInsertRepository {
    private final JdbcTemplate jdbcTemplate;

    public BatchInsertRepository(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void batchInsert(List<Record> recordList) {
        jdbcTemplate.batchUpdate("insert into record(two_level_question_id,result1,result2,result3,student_name,finish_study_years,create_time,is_delete,wenquan_id) values(?,?,?,?,?,?,?,?,?)",
                new BatchPreparedStatementSetter() {
                    @Override
                    public void setValues(PreparedStatement preparedStatement, int i)
                            throws SQLException {
                        Record record = recordList.get(i);
                        preparedStatement.setInt(1, record.getTwoLevelQuestionId());
                        preparedStatement.setString(2, record.getResult1());
                        preparedStatement.setString(3, record.getResult2());
                        preparedStatement.setString(4, record.getResult3());
                        preparedStatement.setString(5, record.getStudentName());
                        preparedStatement.setString(6, record.getFinishYears());
                        preparedStatement.setString(7, record.getCreateTime());
                        preparedStatement.setString(8, record.getIsDelete());
                        preparedStatement.setString(9, record.getWenquanId());

                    }

                    @Override
                    public int getBatchSize() {
                        return recordList.size();
                    }
                });
    }

    //批量删除记录
    public void batchDeleteRecord(List<Record> recordList) {
        jdbcTemplate.batchUpdate("update record set is_delete=? where id=?",
                new BatchPreparedStatementSetter() {
                    @Override
                    public void setValues(PreparedStatement preparedStatement, int i)
                            throws SQLException {
                        Record record = recordList.get(i);
                        preparedStatement.setString(1, "1");
                        preparedStatement.setInt(2, record.getId());
                    }

                    @Override
                    public int getBatchSize() {
                        return recordList.size();
                    }
                });
    }

    //批量GENGXIN记录
    public void batchUpdateRecord(List<Record> recordList) {
        jdbcTemplate.batchUpdate("update record set is_delete=? where id =?",
                new BatchPreparedStatementSetter() {
                    @Override
                    public void setValues(PreparedStatement preparedStatement, int i)
                            throws SQLException {
                        Record record = recordList.get(i);
                        preparedStatement.setString(1, "0");
                        preparedStatement.setInt(2, record.getId());
                    }
                    @Override
                    public int getBatchSize() {
                        return recordList.size();
                    }
                });
    }
}

